<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 20px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>类别列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="ID" ></el-table-column>
      <el-table-column prop="name" label="名称" ></el-table-column>
      <el-table-column prop="parentId" label="父级Id"></el-table-column>
      <el-table-column prop="depth" label="深度"></el-table-column>
      <el-table-column prop="keywords"  label="关键词列表"></el-table-column>
      <el-table-column prop="sort"  label="排列序号"></el-table-column>
      <el-table-column prop="icon"  label="图片路径"></el-table-column>
      <el-table-column  label="是否启用" align="center" width="80">
        <template slot-scope="scope">
          <el-switch
              @change="changeEnable(scope.row)"
              v-model="scope.row.enable"
              :disabled="scope.row.id==1"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="isParent"  width="70" label="是否为负极"></el-table-column>
      <el-table-column  align="center"  label="是否显示在导航栏中">
        <template slot-scope="scope">
          <el-switch
              @change="changeDisplay(scope.row)"
              v-model="scope.row.isDisplay"
              :disabled="scope.row.id==1"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column   label="查看子集" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="mini" :disabled="scope.row.isParent==0"
          @click="showSubCategories(scope.row)">查看子集</el-button>

        </template>
      </el-table-column>

      <el-table-column   label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini"  circle></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="openDeleteConfirm(scope.row)" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-button  style="margin-top: 10px;float: right" v-if="tableData[0].depth != 1"
               @click="showParentCategories()">返回
    </el-button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      currentL1Category: null,
      currentL2Category: null,
      currentDepth: 1,
      currentParentId: 0,
      tableData: []
    }
  },
  methods:{
    showParentCategories() {
      let parentCategory;
      if (this.currentDepth == 3) {
        parentCategory = this.currentL2Category;
        this.currentDepth = 2;
      } else if (this.currentDepth == 2) {
        parentCategory = this.currentL1Category;
        this.currentDepth = 1;
      }

      this.currentParentId = parentCategory.parentId;
      this.loadcategoryList();
    },
    showSubCategories(category) {
      if (category.depth == 1) {
        this.currentL1Category = category;
        this.currentDepth = 2;
      }
      if (category.depth == 2) {
        this.currentL2Category = category;
        this.currentDepth = 3;
      }

      this.currentParentId = category.id;
      this.loadcategoryList();
    },
    changeDisplay(category){
      let enableText= ['不显示在导航栏','显示在导航栏'];
      let url = 'http://localhost:9080/category/'+category.id;
      if (category.isDisplay==1){
        console.log("显示在导航栏")
        url+='/display'
      }else {
        console.log("不显示在导航栏")
        url+='/hidden'
      }
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        console.log(url)
        let responseBody = response.data;
        if (responseBody.state == 10000) {
          let message = '【' +category.name + '】的启用状态改为【' + enableText[category.isDisplay] + '】成功！';
          this.$message({
            message: message,
            type: 'success'
          });
        } else {
          this.$message.error(responseBody.message);
        }
        this.loadcategoryList();
      });
    },
    changeEnable(category){
      let  enableText= ['禁用','启用'];
      let url = 'http://localhost:9080/category/'+category.id;
      if (category.enable==1){
        console.log("将启用品牌")
        url+='/enable'
      }else {
        console.log("将禁用品牌")
        url+='/disable'
      }
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 10000) {
          let message = '将品牌【' +category.name + '】的启用状态改为【' + enableText[category.enable] + '】成功！';
          this.$message({
            message: message,
            type: 'success'
          });
        } else {
          this.$message.error(responseBody.message);
        }
        this.loadcategoryList();
      });
    },
    handleDelete(category){
      let url = 'http://localhost:9080/category/'+category.id+'/delete';
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
          .post(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state!=10000){
          this.$message.error(responseBody.message);
        }
        this.loadcategoryList();
      })
    },
    openDeleteConfirm(category){
      let message='此操作将永久删除该品牌, 是否继续?';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(category);
      }).catch(() => {
      });
    },
    loadcategoryList(parentId){
      let url = 'http://localhost:9080/category/list-by-parent?parentId='+this.currentParentId
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
          .get(url).then((response) =>{
        let responseBody = response.data;
        this.tableData=responseBody.data;
      })
    }
  },
  mounted() {
    console.log('mounted')
    this.loadcategoryList();
  }
}
</script>

